/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#include "Margined.h"

namespace liba
{
namespace lib2d
{
namespace gui
{
namespace view
{

void Margined::render( hard::Renderer * rend, objects::ImageSimple * img, double width, double height )
{
	//stretch borders
	Rect area( rect.x + margins.left(), rect.y, rect.width - margins.right() - margins.left(), margins.top() );
	img->fill_rect( rend, Rect( margins.left(), 0, width - margins.right() - margins.left(), margins.top() ), area );

	area = Rect( rect.x + margins.left(), rect.get_bottom() - margins.bottom(), rect.width - margins.right() - margins.left(), margins.bottom() );
	img->fill_rect( rend, Rect( margins.left(), height - margins.bottom(), width - margins.right() - margins.left(), margins.bottom() ), area );

	area = Rect( rect.x, rect.y + margins.top(), margins.left(), rect.height - margins.bottom() - margins.top() );
	img->fill_rect( rend, Rect( 0, margins.top(), margins.left(), height - margins.top() - margins.bottom() ), area );

	area = Rect( rect.get_right() - margins.right(), rect.y + margins.top(), margins.right(), rect.height - margins.bottom() - margins.top() );
	img->fill_rect( rend, Rect( width - margins.right(), margins.top(), margins.right(), height - margins.top() - margins.bottom() ), area );

	//stretch client area
	area = Rect( rect.x + margins.left(), rect.y + margins.top(), rect.width - margins.left() - margins.right(), rect.height - margins.bottom() - margins.top() );
	img->fill_rect( rend, Rect( margins.left(), margins.top(), width - margins.right() - margins.left(), height - margins.top() - margins.bottom() ), area );

	//draw corners
	area = Rect( rect.x, rect.y, margins.left(), margins.top() );
	img->blit( rend, Vector2( 0, 0 ), area );

	area = Rect( rect.x, rect.get_bottom() - margins.bottom(), margins.left(), margins.bottom() );
	img->blit( rend, Vector2( 0, height - margins.bottom() ), area );

	area = Rect( rect.get_right() - margins.right(), rect.y, margins.right(), margins.top() );
	img->blit( rend, Vector2( width - margins.right(), 0 ), area );

	area = Rect( rect.get_right() - margins.right(), rect.get_bottom() - margins.bottom(), margins.right(), margins.bottom() );
	img->blit( rend, Vector2( width - margins.right(), height - margins.bottom() ), area );
}

bool Margined::on_attribute( xml::Provider * prov, const std::string & name, const std::string & value )
{
	if( name == "Rect" )
		return converter::convert( value, &rect );
	if( name == "Margins" )
		return converter::convert( value, &margins );

	return false;
}

} //namespace view
} //namespace gui
} //namespace lib2d
} //namespace liba

